//
//  LMTabBarItem.m
//  LMTabBarControllerKit
//
//  Created by mengmenglu on 6/24/16.
//  Copyright © 2016 Hangzhou TaiXuan Network Technology Co., Ltd. All rights reserved.
//

#import "LMTabBarItem.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

#define  NORMAL_IMAGE_NAME          @"trHomeDown_normal.png"
#define  SELECT_IMAGE_NAME          @"trHomeDown_selected.png"


@interface LMTabBarItem ()

@property (nonatomic,strong) UILabel *tabBarTitleLabel;
@property (nonatomic,strong) UIImageView *normalImageView;
@property (nonatomic,strong) UIImageView *selectImageView;

@end


@implementation LMTabBarItem

#pragma mark - LifeCycle 
- (id)init {
    self = [super init];
    if (self) {
        [self initParam];
        [self initView];
    }
    return self;
}


#pragma mark - Public Method
- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        self.tabBarTitleLabel.textColor = _tabBarItemModel.selectedTextColor;
        self.normalImageView.hidden = YES;
        self.selectImageView.hidden = NO;
    }
    else {
        self.tabBarTitleLabel.textColor = _tabBarItemModel.normalTextColor;
        self.normalImageView.hidden = NO;
        self.selectImageView.hidden = YES;
    }
}


- (void)setTabBarItemModel:(LMTabBarItemModel *)tabBarItemModel {
    _tabBarItemModel = tabBarItemModel;
}


- (void)updateItemView {
    [self lm_masonry];
    [self setUpView];
}


#pragma mark - Private Method
- (void)initView {
    [self addSubview:self.tabBarTitleLabel];
    [self addSubview:self.normalImageView];
    [self addSubview:self.selectImageView];
    [self lm_masonry];
    [self setUpView];
}


- (void)initParam {
    
    _tabBarItemModel = [[LMTabBarItemModel alloc]
                                           initWithTitle:@"tabbarItemTitle"
                                           normalTextColor:[UIColor blackColor]
                                           selectTextColor:TH27A1E5
                                           normalImage:[UIImage imageNamed:NORMAL_IMAGE_NAME]
                                           selectedImage:[UIImage imageNamed:SELECT_IMAGE_NAME]
                                           normalImageSize:DEFAULT_TABBARITEM_SIZE
                                           selectedImageSize:DEFAULT_TABBARITEM_SIZE
                                           normalImageUrl:nil selectImageUrl:nil];
}


- (void)lm_masonry {
    
    [self.tabBarTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self).with.offset(-3);
        make.height.mas_equalTo(@(11));
    }];
    
    
    [self.normalImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.tabBarTitleLabel.mas_top).with.offset(-3);
        make.width.mas_equalTo(@(_tabBarItemModel.normalImageSize.width));
        make.height.mas_equalTo(@(_tabBarItemModel.normalImageSize.height));
    }];
    
    
    [self.selectImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(_tabBarTitleLabel.mas_top).with.offset(-3);
        make.width.mas_equalTo(@(_tabBarItemModel.selectedImageSize.width));
        make.height.mas_equalTo(@(_tabBarItemModel.selectedImageSize.height));
    }];
    
}



- (void)setUpView {
    [self.tabBarTitleLabel setText:_tabBarItemModel.title];
    self.selectImageView.hidden = YES;
    if (_tabBarItemModel.normalImageURL && _tabBarItemModel.selectedImageURL) {
        [self.normalImageView sd_setImageWithURL:[NSURL URLWithString:_tabBarItemModel.normalImageURL]];
        [self.selectImageView sd_setImageWithURL:[NSURL URLWithString:_tabBarItemModel.selectedImageURL]];
    } else {
        self.normalImageView.image = _tabBarItemModel.normalImage;
        self.selectImageView.image = _tabBarItemModel.selectedImage;
    }
}


#pragma mark - Getter Method
- (UILabel *)tabBarTitleLabel {
    if (!_tabBarTitleLabel) {
        _tabBarTitleLabel = [[UILabel alloc] init];
        _tabBarTitleLabel.textAlignment = NSTextAlignmentCenter;
        _tabBarTitleLabel.textColor = [UIColor blackColor];
        _tabBarTitleLabel.font = [UIFont systemFontOfSize:10];
    }
    return _tabBarTitleLabel;
}


- (UIImageView *)normalImageView {
    if (!_normalImageView) {
        _normalImageView = [[UIImageView alloc] init];
        //_normalImageView.backgroundColor = [UIColor redColor];
    }
    return _normalImageView;
}


- (UIImageView *)selectImageView {
    if (!_selectImageView) {
        _selectImageView = [[UIImageView alloc] init];
        //_selectImageView.backgroundColor = [UIColor blueColor];
    }
    return _selectImageView;
}


@end
